package org.vidi.amqp.pubsub;

import com.rabbitmq.client.*;
import org.vidi.amqp.common.ConnectionUtils;
import org.vidi.amqp.common.Constants;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * @author vidi
 */
public class LogSubscribe {
    public static void main(String[] args) {
        try (Connection connection = ConnectionUtils.newSingleConnection();
             Channel channel = connection.createChannel()) {

            channel.exchangeDeclare(Constants.EXCHANGE_NAME, BuiltinExchangeType.FANOUT);
            String queueName = channel.queueDeclare().getQueue();
            channel.queueBind(queueName, Constants.EXCHANGE_NAME, "");

            DeliverCallback callback = (consumerTag, delivery) -> {
                String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
                System.out.println(message);
            };

            channel.basicConsume(queueName, true, callback, consumerTag -> {});
            Thread.sleep(10000);
        } catch (TimeoutException | IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
